﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Common;
using V5_BLL;
using V5_Model;

namespace V5_EShop.Admin {
    public partial class Admin_Article : BasicPage {
        private const string vArticle = "vArticle";

        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]).Trim().ToLower();
                Bind_DropClassList();
                switch (Action) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList() {
            this.MultiViewList.ActiveViewIndex = 0;
            string strWhere = "";
            if (ViewState[vArticle] != null && InputHelper.SetString(ViewState[vArticle]) != "") {
                strWhere = InputHelper.SetString(ViewState[vArticle]);
            }
            int count = 0;
            DataSet ds = new BLLArticle().GetList(Pager.PageSize, Pager.CurrentPageIndex, strWhere, ref count);
            Pager.RecordCount = count;
            this.gvList.DataSource = ds;
            this.gvList.DataBind();
        }

        private void Bind_DropClassList() {
            this.ddlClassID.DataSource = new BLLArticleClass().GetDropDownList("请选择", 0);
            this.ddlClassID.DataTextField = "ClassName";
            this.ddlClassID.DataValueField = "ClassID";
            this.ddlClassID.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            this.Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetInt(e.CommandArgument);
            switch (eName.Trim().ToLower()) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;
            ModelArticle model = new BLLArticle().GetModel(id);
            if (model != null) {
                this.txtID.Value = model.ID.ToString();
                this.txtTitle.Text = model.Title;
                this.txtKeys.Text = model.KeyWords;
                this.ddlClassID.SelectedIndex = this.ddlClassID.Items.IndexOf(this.ddlClassID.Items.FindByValue(model.ClassID.ToString()));
                this.txtCopyFrom.Text = model.CopyFrom;
                this.txtAuthor.Text = model.Author;
                this.txtContent.Text = model.Content;
                this.txtShortContent.Text = model.ShortContent;
                this.chkLinkUrl.Checked = model.IsUrl.ToString() == "1" ? true : false;
                this.txtLinkUrl.Text = model.LinkUrl;
                this.txtDefaultPicUrl.Text = model.DefaultPicUrl;
                this.txtHits.Text = model.Hits.ToString();
                this.chkPassed.Checked = model.Passed.ToString() == "1" ? true : false;

                this.lblTitle.Text = "修改文章";
            }
        }

        private void Bind_Delete(int id) {
            new BLLArticle().Delete(id);
            this.Bind_gvList();
        }

        protected void btnSearch_Click(object sender, EventArgs e) {
            string sTitle = InputHelper.SetString(this.txtSearchText.Text);
            if (sTitle != "") {
                ViewState[vArticle] = " Title like '%" + sTitle + "%' ";
            }
            Bind_gvList();
        }

        protected void btnAll_Click(object sender, EventArgs e) {
            BLLArticle bll = new BLLArticle();
            int opType = InputHelper.SetInt(this.ddlAll.SelectedValue);
            if (opType == 0) {
                Pr.RegClientAlert(this, "请选择一个操作类型!");
                return;
            }
            foreach (System.Web.UI.WebControls.GridViewRow item in this.gvList.Rows) {
                CheckBox chk = (CheckBox)item.FindControl("chkitem");
                int id = InputHelper.SetInt(item.Cells[1].Text);
                if (chk.Checked) {
                    switch (opType) {
                        case 1:
                            bll.Delete(id);
                            this.Bind_gvList();
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int ID = int.Parse(this.txtID.Value);
            int ChannelID = 0;
            string Title = this.txtTitle.Text;
            string KeyWords = this.txtKeys.Text;
            int ClassID = InputHelper.SetInt(this.ddlClassID.SelectedValue);
            string CopyFrom = this.txtCopyFrom.Text;
            string Author = this.txtAuthor.Text;
            string Editor = InputHelper.SetString(Request.Cookies["adminname"].Value);
            string Content = this.txtContent.Text;
            string ShortContent = this.txtShortContent.Text;
            int IsUrl = this.chkLinkUrl.Checked ? 1 : 0;
            string LinkUrl = this.txtLinkUrl.Text;
            string DefaultPicUrl = this.txtDefaultPicUrl.Text;
            int Hits = InputHelper.SetInt(this.txtHits.Text);
            string CutomField = "";
            int Passed = this.chkPassed.Checked ? 1 : 0;

            DateTime AddDateTime = DateTime.Now;
            DateTime UpDateTime = DateTime.Now;
            BLLArticle bll = new BLLArticle();
            ModelArticle model = new ModelArticle();
            model.ChannelID = ChannelID;
            model.Title = Title;
            model.KeyWords = KeyWords;
            model.ClassID = ClassID;
            model.CopyFrom = CopyFrom;
            model.Author = Author;
            model.Editor = Editor;
            model.Content = Content;
            model.ShortContent = ShortContent;
            model.IsUrl = IsUrl;
            model.LinkUrl = LinkUrl;
            model.DefaultPicUrl = DefaultPicUrl;
            model.Hits = Hits;
            model.CutomField = CutomField;
            model.Passed = Passed;
            model.AddDateTime = AddDateTime;
            model.UpDateTime = UpDateTime;

            if (ID == 0) {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (ID > 0) {
                model.ID = ID;
                bll.Update(model);
            }
            this.Bind_gvList();
        }
    }
}
